package com.yonyou.crm.bpub.fastrecord.appctrl;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.crm.common.base.AppBaseCtrl;
import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.rest.constant.AppCodeConstant;
import com.yonyou.crm.common.rest.util.RequestUtil;
import com.yonyou.crm.common.rest.util.ResponseUtil;
import com.yonyou.crm.sact.actrecord.entity.ActRecordTypeEnum;
import com.yonyou.crm.sact.actrecord.entity.ActRecordVO;
import com.yonyou.crm.sact.actrecord.entity.FastRecordTypeEnum;
import com.yonyou.crm.sact.actrecord.rmitf.IActRecordRmService;

@RestController
@RequestMapping(value = "/{version}/mbpub/fastrecords")
public class AppFastrecordCtrl extends AppBaseCtrl {

	@Autowired
	IActRecordRmService service;
	
	/**
	 * 新增与修改
	 * @param version
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value="/{id}", method = RequestMethod.POST)
	public Object save(@PathVariable("version") String version, @PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObject = RequestUtil.getParamObject(request);
		
		String type = paramObject.getString("type");
		String objid = paramObject.getString("objid");
		Map<String, String> datamap = (Map<String, String>) paramObject.get("datamap");
		String recordTypeStr = datamap.get("type");
		Integer recordType = 0;
		if (FastRecordTypeEnum.PHONE_RECORD.getName().equals(recordTypeStr)) {
			recordType = FastRecordTypeEnum.PHONE_RECORD.getValue();
		} else if (FastRecordTypeEnum.MAIL_RECORD.getName().equals(recordTypeStr)) {
			recordType = FastRecordTypeEnum.MAIL_RECORD.getValue();
		} else if (FastRecordTypeEnum.OTHER_RECORD.getName().equals(recordTypeStr)) {
			recordType = FastRecordTypeEnum.OTHER_RECORD.getValue();
		}
		
		ActRecordVO record = new ActRecordVO();
		record.setRecordType(ActRecordTypeEnum.QUICKADD.getValue());
		record.setQuickAddType(recordType);
		record.setContent(datamap.get("content"));
		record.setObjType(Integer.valueOf(type));
		record.setObjId(Long.valueOf(objid));
		if (datamap.containsKey("date") && !(datamap.get("date").equals(""))) {
			record.setNextDate(transformDateFromStr(datamap.get("date"), "yyyy-MM-dd"));
		}
		record.setNextContent(datamap.get("followcontent"));
		
		String errorMsg = insertValidator(record);
		if (!errorMsg.equals("")) {
			throw new CrmMessageException(AppCodeConstant.ServerError, errorMsg);
		}
		
		Map<String, Object> param = new HashMap<String, Object>();
		setBizType(paramObject, param);
		
		if (id.equals(0L)) {
			record = service.save(record);
		} else {
			record.setId(id);
			record = service.update(record);
		}
		
		if (record.getId() == null) {
			throw new CrmMessageException(AppCodeConstant.ServerError, "获取自增主键失败，未保存成功");
		}
		return ResponseUtil.getObject();
	}
	
	/**
	 * 新增校验
	 * @param record
	 * @return
	 */
	private String insertValidator(ActRecordVO record) {
		String Message = "";
		if (record.getObjType() == null || record.getObjType() == 0) {
			Message = "对象类型不能为空";
        }
		if (record.getObjId() == null || record.getObjId() == 0) {
			Message = "对象Id不能为空";
        }
		if (record.getQuickAddType() == null || record.getQuickAddType() == 0) {
			Message = "快速记录类型不能为空";
		}
		if (!StringUtils.isNotBlank(record.getContent())) {
			Message = "快速记录内容不能为空";
        }
		return Message;
	}
}
